<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType = "text/html; charset=UTF-8" %>

<html>
<head>

<c:set var="cssFileName">
	<c:out value="${crudViewMeta.cssFileName}" default="default.css"/>
</c:set>
<link rel="stylesheet" href="<c:url value='/css/${cssFileName}'/>" type="text/css">

<title><c:out value="${crudMeta.title}"/>数据补录页面</title>
<script type="text/javascript">
<!--提交保存动作-->
function doSave() 
{    
	handCraftForm.submitType.value="save";
	handCraftForm.submit();
}

function doSubmit() 
{    
	handCraftForm.submitType.value="submit";
	handCraftForm.submit();
}

</script>
</head>

<body>
<!--先计算各种状态的数量，如初始态，保存态，提交态，审核通过态和审核退回态的数量-->
<c:set var="total" value="0"/>
<c:set var="initState" value="0"/>
<c:set var="saveState" value="0"/>
<c:set var="submitState" value="0"/>
<c:set var="auditPassedState" value="0"/>
<c:set var="auditRejectState" value="0"/>

<c:forEach items="${listData}" var="rowItem" varStatus="s">
	<c:set var="total"><c:out value="${total+1}"/></c:set>
	<c:if test="${rowItem['HANDFLAG'] == 0}">
		<c:set var="initState"><c:out value="${initState+1}"/></c:set>
	</c:if>
	<c:if test="${rowItem['HANDFLAG'] == 1}">
		<c:set var="saveState"><c:out value="${saveState+1}"/></c:set>
	</c:if>
	<c:if test="${rowItem['HANDFLAG'] == 2}">
		<c:set var="submitState"><c:out value="${submitState+1}"/></c:set>
	</c:if>
	<c:if test="${rowItem['HANDFLAG'] == 3}">
		<c:set var="auditPassedState"><c:out value="${auditPassedState+1}"/></c:set>
	</c:if>
	<c:if test="${rowItem['HANDFLAG'] == 4}">
		<c:set var="auditRejectState"><c:out value="${auditRejectState+1}"/></c:set>
	</c:if>
</c:forEach>

<!--如果已经有补录数据已经被审核通过-->
<c:if test="${auditPassedState != 0}">
<table class="top" align="center">
	<tr><td class="headtitle">审核通过的数据</td></tr>
</table>
<table class="content" width="<c:out value='${crudViewMeta.listTableWidth}'/>">
	<tr>
		<c:forEach items="${crudMeta.columnMetas}" var="col">
	  		<c:if test="${col.showInEditForm}">
	  			<td class="headtitle" nowrap><c:out value="${col.title}"/>
	  			<c:if test="${!col.nullable}"><span class="notNullHint">*</span></c:if>
	  			</td>
	  		</c:if>
	  	</c:forEach>
	</tr>
	<c:forEach items="${listData}" var="rowItem" varStatus="s">
		<c:if test="${rowItem['HANDFLAG'] == 3}">
		<tr>
			
			<c:forEach items="${crudMeta.columnMetas}" var="col">
				<c:if test="${col.showInEditForm}">
					<td class="listhead">
						<c:if test="${empty col.dictName || !col.needDictReplace}">
							<c:out value="${rowItem[col.colName]}"/>
						</c:if>
						<c:if test="${not empty col.dictName && col.needDictReplace}">
							<c:out value="${col.dictMap[rowItem[col.colName]]}"/>
						</c:if>	
					</td>
				</c:if>
			</c:forEach>
		</tr>
		</c:if>
	</c:forEach>
</table>
<br>
</c:if>

<!--如果已经有提交过的数据但是还未经审核-->
<c:if test="${submitState != 0}">
<table class="top" align="center">
	<tr><td class="headtitle">提交但未经审核的数据</td></tr>
</table>
<table class="content" width="<c:out value='${crudViewMeta.listTableWidth}'/>">
	<tr>
		<c:forEach items="${crudMeta.columnMetas}" var="col">
	  		<c:if test="${col.showInEditForm}">
	  			<td class="headtitle" nowrap><c:out value="${col.title}"/>
	  			<c:if test="${!col.nullable}"><span class="notNullHint">*</span></c:if>
	  			</td>
	  		</c:if>
	  	</c:forEach>
	</tr>
	<c:forEach items="${listData}" var="rowItem" varStatus="s">
		<c:if test="${rowItem['HANDFLAG'] == 2}">
		<tr>
			
			<c:forEach items="${crudMeta.columnMetas}" var="col">
				<c:if test="${col.showInEditForm}">
					<td class="listhead">
						<c:if test="${empty col.dictName || !col.needDictReplace}">
							<c:out value="${rowItem[col.colName]}"/>
						</c:if>
						<c:if test="${not empty col.dictName && col.needDictReplace}">
							<c:out value="${col.dictMap[rowItem[col.colName]]}"/>
						</c:if>	
					</td>
				</c:if>
			</c:forEach>
		</tr>
		</c:if>
	</c:forEach>
</table>
<br>
</c:if>

<form name="handCraftForm" action="<c:url value='/handCraft.do'/>" method="POST"/>
    <input type="hidden" name="nrOfElements" value="<c:out value='${initState + saveState + auditRejectState}'/>">
	<input type="hidden" name="name" value="<c:out value='${crudMeta.beanName}'/>"/>
	<!--补录数据处理的方式，当原有补录数据为空时，则增加(add)；如果已有数据，则更新(update)-->
	<input type="hidden" name="handCraftDataDoesNotExist" value="<c:if test='${total==0}'>true</c:if><c:if test='${total != 0}'>false</c:if>">
	<!--提交类型：如保存，还是提交-->
	<input type="hidden" name="submitType">
	<c:forEach items="${crudMeta.otherWantedParameters}" var="wantedParam">
		<input type="hidden" name="<c:out value='${wantedParam}'/>" value="<c:out value='${param[wantedParam]}'/>">
	</c:forEach>

<!--计算表单的行数-->
<c:set var="formRowCount" value="0"/>
<c:forEach items="${command.records}">	
	<c:set var="formRowCount"><c:out value="${formRowCount+1}"/></c:set>
</c:forEach>

<c:if test="${formRowCount != 0}">
<table class="top" align="center">
	<tr><td class="headtitle">对&nbsp;[<c:out value="${crudMeta.title}"/>]&nbsp;进行数据补录</td></tr>
</table>


<table class="content" width="<c:out value='${crudViewMeta.listTableWidth}'/>">
    <tr>
    	<td>&nbsp;</td>
	  	<!--列名称-->
		<c:forEach items="${crudMeta.columnMetas}" var="col">
	  		<c:if test="${col.showInEditForm}">
	  			<td class="headtitle" nowrap><c:out value="${col.title}"/>
	  			<c:if test="${!col.nullable}"><span class="notNullHint">*</span></c:if>
	  			</td>
	  		</c:if>
	  	</c:forEach>
	</tr>
	
	<c:set var="nrOfElements" value="0"/>
	<c:set var="validateErrorIndex" value="-1"/>
	
	<c:forEach items="${command.records}" var="rowItem" varStatus="s">
	<c:if test="${rowItem.mapContent['HANDFLAG']==0 || rowItem.mapContent['HANDFLAG']==1 ||rowItem.mapContent['HANDFLAG']==4}">
	<tr>
		
		<c:if test="${rowItem.mapContent['HANDFLAG']==0}">
			<td class="hintGreen">-></td>
		</c:if>
		<c:if test="${rowItem.mapContent['HANDFLAG']==1}">
			<td class="hintBlue">-></td>
		</c:if>
		<c:if test="${rowItem.mapContent['HANDFLAG']==4}">
			<td class="hintRed">-></td>
		</c:if>
		
		<c:forEach items="${crudMeta.columnMetas}" var="col">
			
			<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
				<c:set var="errorPersent"><c:if test="${not empty status.errorMessage}">true</c:if></c:set>
				<c:if test="${not empty errorPersent}">
					<c:set var="validateErrorIndex"><c:out value="${validateErrorIndex + 1}"/></c:set>
				</c:if>
			</spring:bind>
			
			<c:if test="${!col.derived}">			
				<spring:bind path="command.records[${nrOfElements}].mapContentBefore[${col.colName}]">
					<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
				</spring:bind>
			</c:if>
				
			<c:if test="${col.showInEditForm}">
				
				<!--如果该列是引申而来的，显示数据的同时也需要提供表单元素，否则在校验不通过的时候无法显示其值-->
				<c:if test="${col.derived}">
					<td class="listhead">
						<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
							<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
						</spring:bind>
						<c:if test="${empty col.dictName || !col.needDictReplace}">
							<c:out value="${command.records[nrOfElements].mapContent[col.colName]}"/>
						</c:if>
						<c:if test="${not empty col.dictName && col.needDictReplace}">
							<c:out value="${col.dictMap[command.records[nrOfElements].mapContent[col.colName]]}"/>
						</c:if>	
					</td>
				</c:if>
				
				<c:if test="${!col.derived}">
					<c:if test="${!col.editable}">
					<td class="listhead">
					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
						<c:if test="${not empty col.dictName}">
							<c:out value='${col.dictMap[status.value]}'/>
						</c:if>
						<c:if test="${empty col.dictName}">
							<c:out value='${status.value}'/>
						</c:if>
					</spring:bind>
					</td>
					</c:if>
		
					<c:if test="${col.editable && empty col.dictName}">
					<td>
					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<input type="text" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
					</spring:bind>
					<c:if test="${not empty errorPersent}">
						<span class="validateError"><--&nbsp;<c:out value="${validateErrorIndex+1}"/></span>
					</c:if>
					
					</td>
		      		</c:if>
  		
	  				<c:if test="${col.editable && not empty col.dictName}">
	  				<td>
	  					<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
						<select name="<c:out value='${status.expression}'/>">
							<c:if test="${!col.primaryKey && col.nullable}"><option value="">(未选取)</option></c:if>
							<c:forEach var="map" items="${col.dictMap}">
							<option value='<c:out value="${map.key}"/>' <c:if test="${map.key == status.value}">selected="selected"</c:if>><c:out value="${map.value}"/></option>
							</c:forEach>
						</select>
						</spring:bind>
					<c:if test="${not empty errorPersent}">
						<span class="validateError"><--&nbsp;<c:out value="${validateErrorIndex+1}"/></span>
					</c:if>
					</td>
					</c:if>

				</c:if>
				
			</c:if>
			
			<c:if test="${!col.showInEditForm && !col.derived}">
				<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
					<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>">
				</spring:bind>
			</c:if>
	
		</c:forEach>		
	</tr>
	<c:set var="nrOfElements"><c:out value="${nrOfElements + 1}"/></c:set>
	</c:if>
	</c:forEach>
</table>

<br>
<table>
	<c:forEach items="${crudMeta.columnMetas}" var="col">
	<c:if test="${not empty col.hint}">
		<tr>
			<td><b><c:out value="${col.title}"/>：</b></td>
			<td><span class="hint"><c:out value="${col.hint}"/></span><td>
		</tr>
	</c:if>
	</c:forEach>
</table>

<br>
<table>
	<tr><td colspan=2><b>数据补录提示：</b></td></tr>
	<c:if test="${total == 0}">
		<tr><td class="hintGreen">--></td><td class="hintGreen">表示将要新增的补录数据</td></tr>
	</c:if>
	<c:if test="${saveState != 0}">
		<tr><td class="hintBlue">--></td><td class="hintBlue">表示保存但没有提交审批的数据</td></tr>
	</c:if>
	<c:if test="${auditRejectState != 0}">
		<tr><td class="hintRed">--></td><td class="hintRed">表示被审批退回的数据</td></tr>
	</c:if>
</table>

<br>
<c:if test="${validateErrorIndex >= 0}">
<table width="100%">
	<c:set var="nrOfElements" value="0"/>
	<c:set var="validateErrorIndex" value="-1"/>
	<c:forEach items="${command.records}" varStatus="s">
		<c:forEach items="${crudMeta.columnMetas}" var="col">			
			<spring:bind path="command.records[${nrOfElements}].mapContent[${col.colName}]">
				<c:set var="errorPersent"><c:if test="${not empty status.errorMessage}">true</c:if></c:set>
				<c:if test="${not empty errorPersent}">
					<c:set var="validateErrorIndex"><c:out value="${validateErrorIndex + 1}"/></c:set>
					<tr>
						<span class="validateError">
							<c:out value="${validateErrorIndex+1}"/>.&nbsp;&nbsp;
							<c:out value="${status.errorMessage}"/>
						</span>
					</tr>
				</c:if>
			</spring:bind>
		</c:forEach>
		<c:set var="nrOfElements"><c:out value="${nrOfElements + 1}"/></c:set>
	</c:forEach>
</table>
<br>
</c:if>

<input type="hidden" name="nrOfElements" value="<c:out value='${nrOfElements}'/>">

<table>
	<tr align="center" valign="center">
		<td align="left" width="50%">
			<input type="button" name="save" value="保存" onclick="javascript:doSave()">
			<input type="button" name="submitData" value="提交" onclick="javascript:doSubmit()">
		</td>
		<td width="50%">
		</td>
	</tr>
</table>

</c:if>
<c:if test="${formRowCount == 0}">
	<br>
	<table>
		<tr><td colspan=2><b>目前无需补录数据</b></td></tr>
	</table>
</c:if>
</form>
</body>
</html>